var express = require("express");
var router = express.Router();
var connection = require("../db/sql");
var user = require("../db/UserSql");
var QcloudSms = require('qcloudsms_js');
let jwt =require('jsonwebtoken')

//查询购物车数据
router.post('./api/selectCart',function(req,res,next){
  //token
  let token = req.headers.token;
  let tokenObj = jwt.decode(token);
  //查询用户
  connection.query(`select * from user where tel =${tokenObj.name}`,function(error,results){
    //用户id
    let uId = results[0].id;
    //查询购物车
    connection.query(`select * from goods_cart where uId =${uId}`,function(error,results){     
      res.send({
        data:{
          code:200,
          success:true,
          data:results
        }
      })
    })
  })
})
//添加购物车数据
router.post('/api/addCart',function(req,res,next){
  res.send({
    data:{
      a:abc
    }
  })
})

// router.post('/api/addCart',function(req,res,next){
//   //后端接受前端的参数
//   let goodsId =  req.body.goodsId
//   //token
//   let token = req.headers.token;
//   let tokenObj = jwt.decode(token);
//   console.log('token解析 添加购物车',tel);
//   //查询用户
//   connection.query(`select * from user where tel =${tokenObj.name}`,function(error,results){
//     //用户id
//     let id = results[0].id;

//     console.log('在addcart中',results[0]);
//     //查询商品
//     connection.query(`select * from goods_list where id=${goodsId}`,function(error,results){
//       console.log(results);
//       let goodsName = results[0].name;
//       let goodsPrice = results[0].price;
//       let goodsImgUrl = results[0].imgUrl;
//         connection.query(`insert into goods_cart (uId,goods_id,goods_name,goods_price,goods_num,goods_imgUrl) values ("${uId}","${goodsId}","${goodsName}","${goodsPrice}","1","${goodsImgUrl}") `,function(error,results){
//           res.send({
//             data:{
//               code:200,
//               msg:'添加成功',
//               success:'添加成功'
//             }
//           })
//         })
//     })
//   })
// })
//修改密码
router.post('/api/recovery',function(req,res,next){
  let params = {
    userTel:req.body.phone,
    userPwd:req.body.pwd
  }
  //查询用户是否存在
  connection.query( user.queryUserTel( params ),function(error,results){
   //某一条记录数 id
    let id = results[0].id;
    let pwd = results[0].pwd
    connection.query(`update user set pwd = replace(pwd,'${pwd}'),'${params.userPwd}' where id =${id}`,function(error,results){
      res.send({
        code:0,
        data:{
          msg:'修改成功',
          success:true
        }
      })
    })
  
  })
})
//查询用户是否存在
router.post('/api/selectUser',function(req,res,next){
  let params = {
    userTel:req.body.phone,
  }
  connection.query( user.queryUserTel( params ),function(error,results){
    if( results.length>0){
      res.send({
        code:200,
        data:{
          success:true
        }
      })
    }else{
      res.send({
        code:0,
        data:{
          success:false,
          msg:'此用户不存在'
        }
      })
    }
  })
})
//注册
router.post('/api/register',function(req,res,next){
  let params = {
    userTel:req.body.phone,
    userPwd:req.body.pwd
  }
    //查询用户是否存在
    connection.query( user.queryUserTel( params ),function(error,results){
      if(error) throw error;
      //用户存在
      if( results.length>0){
        res.send({
          code:200,
          data:{
            success:true,
            msg:'登入成功',
            data:results[0]
          }
        })
      }else{
        //不存在新增一条数据
        connection.query(user.inserData(params),function(error,results){
          connection.query(user.queryUserTel(params) , function(e,r){
            res.send({
              code:200,
              data:{
                success:true,
                msg:'登入成功',
                data:r[0]
              }
            })
          })
        })
      }
    })
})

//增加一个用户
router.post('/api/addUser',function(req,res,next){
  let params = {
    userTel:req.body.phone
  }
  //查询用户是否存在
  connection.query( user.queryUserTel( params ),function(error,results){
    if(error) throw error;
    //用户存在
    if( results.length>0){
      res.send({
        code:200,
        data:{
          success:true,
          msg:'登入成功',
          data:results[0]
        }
      })
    }else{
      //不存在新增一条数据
      connection.query(user.inserData(params),function(error,results){
        connection.query(user.queryUserTel(params) , function(e,r){
          res.send({
            code:200,
            data:{
              success:true,
              msg:'登入成功',
              data:r[0]
            }
          })
        })
      })
    }
  })
})

//发送短信验证码
router.post('/api/code',function(req,res,next){

  let tel = req.body.phone
  //短信应用SDK Appid
  var appid = 1400187558
  //短信应用SDK AppKey
  var appkey = "dc9dc3391896235ddc2325685047edc7";
  //需要发送短信的手机号码
  var phoneNumbers = [tel];
  //短信末班ID，需要在短信应用中申请
  var templateId = 285590;
  //签名
  var smsSign = "三人行慕课"
  //实例化QcloudSms
  var qcloudsms = QcloudSms(appid,appkey)
  function callback(err,ress,resData){
    if(err){
        console.log(err);
    }else{
      res.send({
        code:200,
        data:{
          success:true,
          data:ress.req.body.params[0]
        }
      })
    }
  }


  var ssender = qcloudsms.SmsSingleSender();
  //这个变量就是往手机上  发送的短信
  var params = [ Math.floor( Math.random()*(9999-1000))+1000 ];
  ssender.sendWithParam(86,phoneNumbers[0],templateId,params,smsSign,"","",callback);



})



//登录
router.post("/api/login", function (req, res, next) {
  //后端接受前端传输的值
  let params = {
    userTel: req.body.userTel,
    userPwd: req.body.userPwd,
  };

  //查询用户手机号是否存在
  connection.query(user.queryUserTel(params), function (error, results) {
    //手机号存在
    if (results.length > 0) {
      connection.query(user.queryUserPwd(params), function (error, results) {
        if (results.length > 0) {
          //手机号和密码都对
          res.send({
            code: 200,
            data: {
              success: true,
              msg:'登录成功',
              data: results[0],
            },
          });
        } else {
          //密码不对
          res.send({
            code: 302,
            data: {
              success: false,
              msg: "密码不对",
            },
          });
        }
      });
    } else {
      //不存在
      res.send({
        code: 301,
        data: {
          success: false,
          msg: "手机号不不存在",
        },
      });
    }
  });
});

//查询商品id的数据
router.get("/api/goods/id", function (req, res, next) {
  let id = req.query.id;
  connection.query(
    "select * from goods_list where id=" + id + "",
    function (error, results) {
      if (error) throw Error;
      res.json({
        code: 0,
        data: results[0],
      });
    }
  );
});
//分类的接口
router.get("/api/goods/list", function (req, res, next) {
  res.send({
    code: 0,
    data: [
      {
        //一级
        id: 0,
        name: "推荐",
        data: [
          {
            //二级
            id: 0,
            name: "推荐",
            list: [
              //三级
              {
                id: 0,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "贴关于",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
      {
        //一级
        id: 1,
        name: "路查",
        data: [
          {
            //二级
            id: 0,
            name: "路查",
            list: [
              //三级
              {
                id: 0,
                name: "龙津",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "碧罗村",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "雀舌",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "安吉子宫",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "白茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "黑茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "红茶",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
      {
        //一级
        id: 2,
        name: "乌龙",
        data: [
          {
            //二级
            id: 0,
            name: "掉毛",
            list: [
              //三级
              {
                id: 0,
                name: "自己",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "哈哈",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "尼玛",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "狗日",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "白茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "黑茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "红茶",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
      {
        //一级
        id: 3,
        name: "乌龙",
        data: [
          {
            //二级
            id: 0,
            name: "掉毛",
            list: [
              //三级
              {
                id: 0,
                name: "自己",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "哈哈",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "尼玛",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "狗日",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "白茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "黑茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "红茶",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
      {
        //一级
        id: 4,
        name: "乌龙",
        data: [
          {
            //二级
            id: 0,
            name: "掉毛",
            list: [
              //三级
              {
                id: 0,
                name: "自己",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "哈哈",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "尼玛",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "狗日",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "白茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "黑茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "红茶",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
      {
        //一级
        id: 2,
        name: "乌龙",
        data: [
          {
            //二级
            id: 0,
            name: "掉毛",
            list: [
              //三级
              {
                id: 5,
                name: "自己",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 1,
                name: "哈哈",
                imgUrl: "./images/list1.jpeg",
              },

              {
                id: 2,
                name: "尼玛",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 3,
                name: "狗日",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 4,
                name: "白茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 5,
                name: "黑茶",
                imgUrl: "./images/list1.jpeg",
              },
              {
                id: 6,
                name: "红茶",
                imgUrl: "./images/list1.jpeg",
              },
            ],
          },
        ],
      },
    ],
  });
});
//查询商品数据接口
router.get("/api/goods/shopList", function (req, res, next) {
  //前端给后端的数据
  let [searchName, roderName] = Object.keys(req.query);
  let [name, order] = Object.values(req.query);

  connection.query(
    'select * from goods_list where name like "%' +
      name +
      '%" order by' +
      orderName +
      " " +
      order +
      "",
    function (error, results) {
      res.send({
        code: 0,
        data: results,
      });
    }
  );
});

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "我就是神" });
});


//首页大红袍的数据
router.get("/api/index_list/1/data/1", function (req, res, next) {
  res.send({
    code: 0,
    data: [
      {
        id: 1,
        type: "adList",
        data: [
          {
            id: 1,
            imgUrl: "./images/dhp.jpeg",
          },
          {
            id: 2,
            imgUrl: "./images/dhp.jpeg",
          },
        ],
      },
      {
        id: 2,
        type: "likeList",
        data: [
          {
            id: 1,
            imgUrl: "./images/like.jpeg",
            name: "间杂茶具套装 工鞥色会吗奥扎  12 检讨",
            price: 299,
          },
          {
            id: 2,
            imgUrl: "./images/like.jpeg",
            name: "间杂茶具套装 工鞥色会吗奥扎  12 检讨",
            price: 299,
          },
          {
            id: 3,
            imgUrl: "./images/like.jpeg",
            name: "间杂茶具套装 工鞥色会吗奥扎  12 检讨",
            price: 299,
          },
        ],
      },
    ],
  });
});
//首页推荐的数据
router.get("/api/index_list/0/data/1", function (req, res, next) {
  res.send({
    code: 0,
    data: {
      topBar: [
        { id: 0, label: "推荐" },
        { id: 1, label: "大红袍" },
        { id: 2, label: "铁观音" },
        { id: 3, label: "绿茶" },
        { id: 4, label: "普洱" },
        { id: 5, label: "茶具" },
        { id: 6, label: "花茶" },
      ],
      data: [
        //这是swiper
        {
          id: 0,
          type: "swiperList",
          data: [
            { id: 0, imgUrl: "./images/swiper1.jpeg" },
            { id: 1, imgUrl: "./images/swiper2.jpeg" },
            { id: 2, imgUrl: "./images/swiper3.jpeg" },
          ],
        },
        //这是icons
        {
          id: 2,
          type: "iconsList",
          data: [
            {
              id: 1,
              title: "喜茶",
              imgUrl: "./images/icons1.png",
            },
            {
              id: 2,
              title: "茶具",
              imgUrl: "./images/icons2.png",
            },
            {
              id: 3,
              title: "领取礼包",
              imgUrl: "./images/icons3.png",
            },
            {
              id: 4,
              title: "自饮茶",
              imgUrl: "./images/icons4.png",
            },
            {
              id: 5,
              title: "官方验证",
              imgUrl: "./images/icons5.png",
            },
          ],
        },
        //爆款推荐
        {
          id: 3,
          type: "recommendList",
          data: [
            {
              id: 1,
              name: "龙井茶叶真牛逼哈哈哈",
              content: "鲜爽甘醇 口粮首选",
              price: "68",
              imgUrl: "./images/recommend.jpeg",
            },
            {
              id: 2,
              name: "龙井茶叶真牛逼哈哈哈",
              content: "鲜爽甘醇 口粮首选",
              price: "68",
              imgUrl: "./images/recommend.jpeg",
            },
          ],
        },
        //猜你喜欢
        {
          id: 4,
          type: "likeList",
          data: [
            {
              id: 1,
              imgUrl: "./images/goods1.jpg",
              name: "龙井",
              price: 89,
            },
            {
              id: 2,
              imgUrl: "./images/goods2.jpg",
              name: "绿茶",
              price: 46,
            },
            {
              id: 3,
              imgUrl: "./images/goods3.jpg",
              name: "红茶",
              price: 546,
            },
            {
              id: 4,
              imgUrl: "./images/goods4.jpg",
              name: "紫茶",
              price: 465,
            },
            {
              id: 5,
              imgUrl: "./images/goods1.jpg",
              name: "龙井",
              price: 89,
            },
            {
              id: 6,
              imgUrl: "./images/goods2.jpg",
              name: "绿茶",
              price: 46,
            },
            {
              id: 7,
              imgUrl: "./images/goods3.jpg",
              name: "红茶",
              price: 546,
            },
            {
              id: 8,
              imgUrl: "./images/goods4.jpg",
              name: "紫茶",
              price: 465,
            },
          ],
        },
      ],
    },
  });
});

module.exports = router;
